﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Xml;
using System.Windows.Navigation;
using System.Xml.Linq;
using System.Diagnostics;
using Microsoft.Phone.Tasks;


namespace OpenGovernment
{
    public class GetData
    {
        HttpWebRequest req;
        public delegate void GotDataHandler(XElement xe);
        public event GotDataHandler GotData;
        public delegate void EpicFailHandler();
        public static event EpicFailHandler EpicFail;
        public GetData(String uri)
        {
            req = (HttpWebRequest)WebRequest.Create(uri);
            req.BeginGetResponse(new AsyncCallback(RequestCallback), null);
        }
        void RequestCallback(IAsyncResult resu)
        {
            try
            {
                HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(resu);
                Thread.CurrentThread.CurrentCulture =
                    new System.Globalization.CultureInfo("el-GR");

                XmlReaderSettings xmlrs =
                    new XmlReaderSettings() { DtdProcessing = System.Xml.DtdProcessing.Ignore };
                XmlReader xmlreader = XmlReader.Create(res.GetResponseStream(), xmlrs);
                xmlreader.MoveToContent();

                XElement xe = (XElement)XElement.ReadFrom(xmlreader);

                GotData(xe);
            }
            catch (WebException)
            {
                EpicFail();
            }
        }
    }
}